/*
 * Getrap
 * Copyright 2008(C) Grzegorz 'milyges' Glinski
 *
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
*/
#ifndef __JOB_H
#define __JOB_H

#include <QTreeWidgetItem>
#include <QObject>
#include <QHttp>
#include <QUrl>
#include <QTimer>
#include <QFile>
#include <QBuffer>

/** Status zadania */
enum jobStatus { jsStopped = 0, jsQueue, jsReadyWait, jsRunning, jsDone, jsError };

/** Klasa identyfikująca zadanie pobierania */
class Job : public QObject, public QTreeWidgetItem
{
 Q_OBJECT
 private:
   /** Tablica zawierająca statusy zadania przyjazne dla uzytkownika */
   static char * mStatusTable[];
   /** Ilość aktualnie uruchomionych zadań */
   static int mActiveJobs;
   /** */
   static QString size2str(int size);
   /** */
   static QString time2str(int secs);
   /** Nazwa pobieranego pliku */
   QString mFile;
   /** Link */
   QUrl mLink;
   /** Status zadania */
   int mStatus;
   /** Używane przy oczekiwaniu */
   int mTimeout;
   int mDone;
   int mDoneOld;
   int mTotal;
   int mSpeed;
   QHttp * mHttp;
   QTimer * mTimer;
   QFile * mOutputFile;
   void httpError(void);
   void setStatus(int status, QString msg = "");
   void startNextJob(void);
 private slots:
   void prepareDone(bool error, bool restart = false);
   void freeSelected(bool error);
   void freeWait(void);
   void downloadDone(bool error);
   void progressUpdate(int done, int total);
   void updateStatus(void);
   void waitForRestart(void);
 public:
   Job(QTreeWidget * parent, QString link);
   ~Job();
   void start(void);
   void stop(void);
   static int activeJobs(void);
   int status(void);
 signals:
   void done(QString fileName);
   void fatalError(QString msg);
};

#endif
